//
//  UIBarButtonItem+Extension.swift
//  weibo
//
//  Created by 杨念川 on 16/8/29.
//  Copyright © 2016年 杨念川. All rights reserved.
//

import UIKit
extension UIBarButtonItem {
    //添加一个构造便利函数
    convenience init(imageName: String? = nil,title: String? = nil,target: Any?, action: Selector?) {
        //调用构造函数初始化
        self.init()
        //创建一个按钮
        let button = UIButton()
        //添加title
        if let t = title {
            //标题
            button.setTitle(t, for: UIControlState.normal)
            //字体大小
            button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            //高亮颜色
            button.setTitleColor(UIColor.lightGray, for: .normal)
            button.setTitleColor(UIColor.orange, for: .highlighted)
        }
        if let img = imageName {
            //设置图片
            button.setImage(UIImage(named: img), for: .normal)
            button.setImage(UIImage(named: "\(img)_highlighted"), for: .highlighted)
        }
        if let act = action {
            //添加点击事件
            button.addTarget(target, action: act, for: .touchUpInside)
        }
        //size
        button.sizeToFit()
        //设置当前按钮为item的customView
        self.customView = button
    }
}
